package com.flipdf2.serialize.ppml.element;

import org.jdom.Element;
import org.jdom.Namespace;

import com.flipdf2.serialize.ppml.PPMLException;

public class PPMLSegmentRef {
	public static final String NAME = "SEGMENT_REF";
	
	private String environment;
	private int index = 1;
	private String ref;
	
	public Element toXML(Namespace defaultNamespace) {
		Element root = null;
		if (defaultNamespace != null) {
			root = new Element(NAME, defaultNamespace);
		} else {
			root = new Element(NAME);	
		}
		
		if (environment != null) 
			root.setAttribute("Environment", environment);
		if (index != -1) 
			root.setAttribute("Index", index + "");
		if (ref != null) 
			root.setAttribute("Ref", ref);
		
		return root;
	}

	public void check() throws PPMLException {
		if (ref == null) {
			throw PPMLException.getInstance(PPMLException.REQUIRED_ATTRIBUTE_LOST, "Ref");
		}
	}
	
	public String getEnvironment() {
		return environment;
	}

	public void setEnvironment(String environment) {
		this.environment = environment;
	}

	public int getIndex() {
		return index;
	}

	public void setIndex(int index) {
		this.index = index;
	}

	public String getRef() {
		return ref;
	}

	public void setRef(String ref) {
		this.ref = ref;
	}
}
